/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JanelaPrincipal.java
 *
 * Created on 26/04/2011, 11:40:10
 */
package gestores.janelas;

import gestores.iteracao.GestorIteracao;
import gestores.iteracao.iteradores.IteradorFiltrado;
import gestores.iteracao.manipuladores.IManipulador;
import gestores.iteracao.manipuladores.marcadagua.ManipuladorMarcaDagua;
import gestores.iteracao.manipuladores.marcadagua.JanelaConfigMarcaDagua;
import gestores.iteracao.manipuladores.rename.JanelaConfigRename;
import gestores.iteracao.manipuladores.rename.ManipuladorRename;
import java.awt.event.KeyEvent;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import kernel.Kernel;

/**
 *
 * @author blaster
 */
public class JanelaPrincipal extends javax.swing.JFrame {

    private File diretorioescolhido = new File(System.getProperty("user.home"));
    private FileListModel modeloListaArquivos;
    private FileListModel modeloListaNavegacao;
    private JanelaConfigBase janelaConfig = null;

    /** Creates new form JanelaPrincipal */
    public JanelaPrincipal() {
        modeloListaNavegacao = new FileListModel(diretorioescolhido);
        modeloListaArquivos = new FileListModel();
        initComponents();
        tfDiretorio.setText(diretorioescolhido.getPath());
        atualizaFileListNavegador();
        pbDone.setMaximum(100);
        pbDone.setMinimum(0);
        atualizarMapipulador("PrintTela");
        this.setLocationRelativeTo(null);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        tfDiretorio = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        btProcurar = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        lsNavigationList = new javax.swing.JList(modeloListaArquivos);
        btAddToFilesList = new javax.swing.JButton();
        btRemoveFromFilesList = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        lsFileList = new javax.swing.JList(modeloListaArquivos);
        jButton1 = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        taLog = new javax.swing.JTextArea();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        btConfigurar = new javax.swing.JButton();
        pbDone = new javax.swing.JProgressBar();
        jSeparator1 = new javax.swing.JSeparator();
        jSeparator2 = new javax.swing.JSeparator();
        lbCaminho = new javax.swing.JLabel();
        lbNArquivosSelecionados = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        tfDiretorio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tfDiretorioActionPerformed(evt);
            }
        });
        tfDiretorio.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                tfDiretorioFocusLost(evt);
            }
        });
        tfDiretorio.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tfDiretorioKeyPressed(evt);
            }
        });

        jLabel1.setText("Diretório:");

        btProcurar.setText("Procurar");
        btProcurar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btProcurarActionPerformed(evt);
            }
        });

        jScrollPane2.setViewportView(lsNavigationList);

        btAddToFilesList.setText("Adicionar");
        btAddToFilesList.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btAddToFilesListActionPerformed(evt);
            }
        });

        btRemoveFromFilesList.setText("Remover");
        btRemoveFromFilesList.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btRemoveFromFilesListActionPerformed(evt);
            }
        });

        jScrollPane3.setViewportView(lsFileList);

        jButton1.setText("Manipular Lista");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel2.setText("Seleção de Arquivos");

        jLabel3.setText("Lista à ser Manipulada");

        taLog.setColumns(20);
        taLog.setRows(5);
        jScrollPane1.setViewportView(taLog);

        jLabel4.setText("Log");

        jLabel5.setText("Manipulador:");

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "PrintTela", "Renomear", "MarcaDagua" }));
        jComboBox1.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jComboBox1ItemStateChanged(evt);
            }
        });

        btConfigurar.setText("Configurar");
        btConfigurar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btConfigurarActionPerformed(evt);
            }
        });

        lbNArquivosSelecionados.setText("0 arquivos selecionados");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 709, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(114, 114, 114)
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(tfDiretorio, javax.swing.GroupLayout.PREFERRED_SIZE, 257, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btProcurar))))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(pbDone, javax.swing.GroupLayout.DEFAULT_SIZE, 709, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 709, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(23, 23, 23)
                        .addComponent(jLabel4))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jSeparator2, javax.swing.GroupLayout.DEFAULT_SIZE, 709, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 276, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(btConfigurar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(18, 18, 18)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                            .addComponent(btRemoveFromFilesList, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                            .addComponent(btAddToFilesList, javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabel5))))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 285, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(188, 188, 188)
                                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 315, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(73, 73, 73)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 277, Short.MAX_VALUE)
                        .addComponent(jLabel3)
                        .addGap(66, 66, 66))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addComponent(lbCaminho, javax.swing.GroupLayout.PREFERRED_SIZE, 258, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(161, 161, 161)
                        .addComponent(lbNArquivosSelecionados, javax.swing.GroupLayout.PREFERRED_SIZE, 264, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(tfDiretorio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btProcurar))
                        .addGap(16, 16, 16))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(31, 31, 31)
                        .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lbNArquivosSelecionados, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lbCaminho, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jScrollPane3)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 297, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(49, 49, 49)
                        .addComponent(btAddToFilesList)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btRemoveFromFilesList)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btConfigurar)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 164, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(pbDone, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tfDiretorioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tfDiretorioActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tfDiretorioActionPerformed

    private void atualizaFileListNavegador() {
        if (!diretorioescolhido.exists()) {
            diretorioescolhido = new File(System.getProperty("user.home"));
            tfDiretorio.setText(System.getProperty("user.home"));
        }
        lsNavigationList.setModel(new FileListModel(diretorioescolhido));
        tfDiretorio.setText(diretorioescolhido.getPath());
        this.lbCaminho.setText(diretorioescolhido.getPath());
    }

    private void tfDiretorioFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_tfDiretorioFocusLost
        diretorioescolhido = new File(tfDiretorio.getText());
        atualizaFileListNavegador();
    }//GEN-LAST:event_tfDiretorioFocusLost

    private void tfDiretorioKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tfDiretorioKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            diretorioescolhido = new File(tfDiretorio.getText());
            atualizaFileListNavegador();
        }
    }//GEN-LAST:event_tfDiretorioKeyPressed

    private void btProcurarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btProcurarActionPerformed
        JFileChooser fc = new JFileChooser();

        // restringe a amostra a diretorios apenas
        fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

        int res = fc.showOpenDialog(null);

        if (res == JFileChooser.APPROVE_OPTION) {
            diretorioescolhido = new File(fc.getSelectedFile().getPath());
            atualizaFileListNavegador();
        }

    }//GEN-LAST:event_btProcurarActionPerformed

    private void btAddToFilesListActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAddToFilesListActionPerformed
        for (Object f : lsNavigationList.getSelectedValues()) {
            if (f instanceof File) {
                modeloListaArquivos.addElement((File) f);
            }
        }
        atualizarFileListSelecionados();
    }//GEN-LAST:event_btAddToFilesListActionPerformed

    private void btRemoveFromFilesListActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btRemoveFromFilesListActionPerformed
        for (Object o : lsFileList.getSelectedValues()) {
            modeloListaArquivos.removeElement(o);
        }
        atualizarFileListSelecionados();
    }//GEN-LAST:event_btRemoveFromFilesListActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        if (janelaConfig != null) {
            IManipulador manipulador = janelaConfig.getManipulador();
            if (manipulador != null && manipulador.valida()) {
                Kernel.getInstance().gestorIteracao = new GestorIteracao(modeloListaArquivos.getFiles());
                Kernel.getInstance().gestorIteracao.setIterador(new IteradorFiltrado(modeloListaArquivos.getFiles(), manipulador.getFilter()));
                Kernel.getInstance().gestorIteracao.setManipulador(manipulador);
                Kernel.getInstance().gestorIteracao.startIteracao();
            } else {
                JOptionPane.showMessageDialog(this, "Manipulador não selecionado ou mal configurado.");
            }
        } else {
            JOptionPane.showMessageDialog(this, "Selecione e configure um manipulador");
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    public void clearLog() {
        taLog.setText("");
    }

    public void atualizarFimIteracao() {
        modeloListaArquivos.clear();
        this.pbDone.setValue(0);
        atualizarFileListSelecionados();
        atualizaFileListNavegador();
    }

    public void addToLog(String linhalog) {
        taLog.append(linhalog);
        if (!linhalog.contains("\n")) {
            taLog.append("\n");
        }
    }

    private void btConfigurarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btConfigurarActionPerformed
        janelaConfig.setVisible(true);
    }//GEN-LAST:event_btConfigurarActionPerformed

    private void jComboBox1ItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jComboBox1ItemStateChanged
        String sel = evt.getItem().toString();
        atualizarMapipulador(sel);
    }//GEN-LAST:event_jComboBox1ItemStateChanged
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btAddToFilesList;
    private javax.swing.JButton btConfigurar;
    private javax.swing.JButton btProcurar;
    private javax.swing.JButton btRemoveFromFilesList;
    private javax.swing.JButton jButton1;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JLabel lbCaminho;
    private javax.swing.JLabel lbNArquivosSelecionados;
    private javax.swing.JList lsFileList;
    private javax.swing.JList lsNavigationList;
    private javax.swing.JProgressBar pbDone;
    private javax.swing.JTextArea taLog;
    private javax.swing.JTextField tfDiretorio;
    // End of variables declaration//GEN-END:variables

    public void setDone(Float aFloat) {
        pbDone.setValue(getIntDone(aFloat));
    }

    private int getIntDone(Float aFloat) {
        return Math.round(aFloat * 100);
    }

    private void atualizarMapipulador(String sel) {
        if (sel.equals("PrintTela")) {
            janelaConfig = null;
        } else if (sel.equals("Renomear")) {
            janelaConfig = new JanelaConfigRename(new ManipuladorRename());
        } else if (sel.equals("MarcaDagua")) {
            janelaConfig = new JanelaConfigMarcaDagua(new ManipuladorMarcaDagua(null));
        }
        if (janelaConfig != null) {
            if (janelaConfig.getManipulador() != null) {
                btConfigurar.setEnabled(true);
            } else {
                btConfigurar.setEnabled(false);
            }
        } else {
            btConfigurar.setEnabled(false);
        }
    }

    private void atualizarFileListSelecionados() {
        lbNArquivosSelecionados.setText(modeloListaArquivos.getSize() + " arquivos selecionados");
    }
}
